package breeze.pixel.weather.cityhistory.view;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupMenu;

import java.util.ArrayList;
import java.util.List;

import breeze.pixel.weather.BaseView;
import breeze.pixel.weather.R;
import breeze.pixel.weather.cityhistory.model.CityHistoryAdapter;
import breeze.pixel.weather.cityhistory.model.CityInfo;
import breeze.pixel.weather.cityhistory.model.MyDatabaseUtils;

public class CityHistory extends BaseView {

    private ListView listview;

    private MyDatabaseUtils utils;

    private List<CityInfo> datas=new ArrayList<CityInfo>();

    @Override
    public void init() {
        listview = find(R.id.morecity_listview);
    }

    @Override
    public void initData() {
        this.utils=MyDatabaseUtils.getInstance(this);
        datas=utils.getAllData();
        if(datas.isEmpty()){
            toast("没有城市数据");
        }
        CityHistoryAdapter adapter=new CityHistoryAdapter(this,datas);
        listview.setAdapter(adapter);
        listview.setOnItemLongClickListener((parent, view, position, id) -> {
            CityInfo info = datas.get(position);
            PopupMenu menu = new PopupMenu(CityHistory.this, view);
            menu.inflate(R.menu.menu_citys_menu);
            menu.show();
            menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    if (item.getItemId() == R.id.menu_citys_delete) {
                        utils.deleteData(info.getCityID());
                        datas.remove(info);
                        adapter.notifyDataSetChanged();
                    } else if (item.getItemId() == R.id.menu_citys_clear){
                        utils.deleteAll();
                        datas.removeAll(datas);
                        adapter.notifyDataSetChanged();
                        finish();
                    } else if (item.getItemId() == R.id.menu_citys_copyID){
                        ClipboardManager manager = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
                        ClipData cData = ClipData.newPlainText("ID",info.getCityID());
                        manager.setPrimaryClip(cData);
                        toast("复制成功");
                    }
                    return false;
                }
            });
            return true;
        });
        listview.setOnItemClickListener((parent, view, position, id) -> {
            CityInfo info = datas.get(position);
            Intent intent = new Intent();
            intent.putExtra("cityCode",info.getCityID());
            CityHistory.this.setResult(1,intent);
            finish();
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_morecity);
        init();
        initData();
    }

}
